/**
 * 延时
 * @param time_ms
 * @returns {Promise<*>}
 */
async function sleep(time_ms = 0) {
  return new Promise(resolve => {
    setTimeout(_ => {
      resolve();
    }, time_ms);
  });
}

function getCid(str = '') {
  str = str.toString();
  return str.split('_').pop();
}

function getName(str = '') {
  str = str.toString();
  let ad = str.split('_');
  ad.pop();
  return ad.join('_');
}

/**
 * 同步队列
 * @param cids 数组
 * @param process 过程函数
 * @param done 完成函数
 * @param sleepTime 延时
 * @returns {Promise<*>}
 */
async function syncQueue(cids = [], process, done, sleepTime = 1000) {
  if (cids.length <= 0) return done && done();
  const cid = cids.shift();
  process && await process(cid);
  await sleep(sleepTime);
  await syncQueue(cids, process, done, sleepTime);
}

module.exports = {
  sleep,
  getCid,
  getName,
  syncQueue,
}
